<?php

//商品分类管理控制器
class CategoryAction extends CommonAction{
	//商品分类管理首页
	public function index(){
		$result = M('category')->select();
		$cate = recursion($result,0,2);
		$this->cate = $cate;
		$this->display();
	}
	
	//添加顶级分类页面
	public function addTopCate(){	
		$this->display();
	}

	//添加子分类
	public function addCate(){
		$type = M('type')->select();
		$this->type = $type;
		$this->display();
	}
	
	//添加分类操作
	public function runAddCate(){
		$data = array(
			'name' => $_POST['name'],
			'pid' => isset($_POST['pid']) ? $_POST['pid'] : 0,
			'tid' => isset($_POST['tid']) ? $_POST['tid'] : 0,
		);
		
		if(M('category')->data($data)->add()){
			$this->success('添加分类成功！');
		}else{
			$this->error('添加分类失败！');
		}
	}

	//编辑分类界面
	public function cateEdit(){
		$id = $this->_get('id');
		$result = M('category')->where("id = $id")->find();
		$this->cate = $result;
		$this->display();
	}

	//执行编辑分类操作
	public function runCateEdit(){
		$data = array(
			'name' => $this->_post('name'),
			'id' => $this->_get('id'),
		);
		
		if(M('category')->data($data)->save()){
			$this->success('编辑分类成功！',U('index'));
		}else{
			$this->error('编辑分类失败！');
		}

	}

	//删除分类操作
	public function cateDel(){
		$id = $this->_get('id');
		if(M('category')->where("id = $id")->delete()){
			$this->success('分类删除成功！');
		}else{
			$this->error('分类删除失败！');
		}
	}

}
?>